package com.scap.authen.controller;


import java.io.IOException;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.scap.authen.entity.Application;
import com.scap.authen.service.ApplicationService;
import com.scap.authen.util.DataTable;

@Controller
@RequestMapping(value = "/application")
public class ApplicationController {

	@Autowired
	private ApplicationService voApplicationService;
	@Autowired
	private ApplicationContext context;
	DataTable voDataTable = new DataTable();

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView application(HttpServletRequest request) {
		Locale voLocale = RequestContextUtils.getLocale(request);
		ModelAndView voModelAndView = new ModelAndView("Application/appManagement","application",new Application());
		voModelAndView.addObject("page", "Application Management");
		voModelAndView.addObject("status", request.getParameter("status"));
		voModelAndView.addObject("message", request.getParameter("message")!=null?request.getParameter("message").replace("_Name_", context.getMessage("class.Application", null, voLocale)):"");
		voModelAndView.addObject("oLanguage", voDataTable.getDataTableLanguage(context,request));
		return voModelAndView;
	}

	@RequestMapping(value = "/applicationDataTables", method = RequestMethod.GET, 
			produces = "application/json; charset=utf-8")
	public @ResponseBody String springPaginationDataTables(
			HttpServletRequest request) throws IOException {
		
		List<Object> applicationsList = voApplicationService.getAllApplication();
		String searchParameter = request.getParameter("sSearch");
		applicationsList = voApplicationService.getListBasedOnSearchParameter(
				searchParameter, applicationsList);

		return voDataTable.springPaginationDataTables(request,applicationsList);
	}

	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView modifyApplication(
			@RequestParam("vaImage") MultipartFile voFile,
			@RequestParam("uploadImgTemp") String voUploadImg,
			@RequestParam("processType") String vaProcessType,
			@Valid Application application,
			BindingResult result,
			HttpServletRequest request) {
		
		Locale voLocale = RequestContextUtils.getLocale(request);
		String vaMessage = "";
		String vaStatus = "";
		
		if(vaProcessType.equals("insert")){
			vaStatus = voApplicationService.insertApplication(application, voFile, result, request);
		}else if(vaProcessType.equals("update")){
			vaStatus = voApplicationService.updateApplication(application, voFile, result, request,voUploadImg);
		}else if(vaProcessType.equals("delete")){
			vaStatus = voApplicationService.deleteApplication(application, request);
		}
		
		if(vaStatus.equals("success")){
			if(vaProcessType.equals("insert") || vaProcessType.equals("update")){
				vaMessage = context.getMessage("alert.save.success", null, voLocale);
			}else{
				vaMessage = context.getMessage("alert.delete.success", null, voLocale);
			}
		}else{
			if(vaProcessType.equals("insert") || vaProcessType.equals("update")){
				vaMessage = context.getMessage("alert.save.fail", null, voLocale);
			}else{
				vaMessage = context.getMessage("alert.delete.fail", null, voLocale);
			}
		}
		
		ModelAndView voModelAndView = new ModelAndView("Application/appManagement");
		voModelAndView.addObject("message", vaMessage.replace("_Name_", context.getMessage("class.Application", null, voLocale)));
		voModelAndView.addObject("status", vaStatus);
		
		return voModelAndView;
	}

}
